﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Walson.Form
{
    public partial class FlightInstrument
    {
        [ToolboxItem(false)]
        public partial class NumericIndicator : UserControl
        {
            private float _Value;
            public double Value
            {
                get
                {
                    return _Value;
                }
                set
                {
                    if (_Value != (float)value)
                    {
                        _Value = (float)value;
                        this.Invalidate();
                    }
                }
            }

            private float _SetValue;
            public double SetValue
            {
                get
                {
                    return _SetValue;
                }
                set
                {
                    if (_SetValue != (float)value)
                    {
                        _SetValue = (float)value;
                        this.Invalidate();
                    }
                }
            }

            private float _GroundAltitudeValue;
            public double GroundAltitudeValue
            {
                get
                {
                    return _GroundAltitudeValue;
                }
                set
                {
                    if (_GroundAltitudeValue != (float)value)
                    {
                        _GroundAltitudeValue = (float)value;
                        this.Invalidate();
                    }
                }
            }

            private bool _IsLeftStyle;
            public bool IsLeftStyle
            {
                get
                {
                    return _IsLeftStyle;
                }
                set
                {
                    if (_IsLeftStyle != value)
                    {
                        _IsLeftStyle = value;
                        this.Invalidate();
                    }
                }
            }

            public Bitmap Image
            {
                get;
                private set;
            }

            private Pen _WhitePen;
            private Pen _RedPen;
            private Pen _PinkPen;

            public NumericIndicator()
            {
                InitializeComponent();
                _WhitePen = new Pen(Brushes.White, 2);
                _RedPen = new Pen(Brushes.Red, 4);
                //_PinkPen = new Pen(Color.FromArgb(239, 123, 247), 2);
                _PinkPen = new Pen(Color.FromArgb(255, 125, 255), 2);
                this.Image = new Bitmap(1, 1);
            }

            public void Paint(PaintEventArgs e)
            {
                OnPaint(e);
            }

            protected override void OnPaint(PaintEventArgs e)
            {                
                using (Bitmap bmp = new Bitmap(this.Width, this.Height))
                //using (Bitmap bmp = new Bitmap(85, 278))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        PointF[] line = new PointF[2];
                        PointF[] polygon;
                        string str;
                        Font font = new Font("Times NewRoman", 12f);
                        float len, height;

                        g.Clear(Color.FromArgb(57, 49, 66));

                        if (IsLeftStyle)
                        {
                            g.TranslateTransform((float)this.Width - 20, (float)this.Value * 2f + this.Height / 2f);

                            polygon = new PointF[4];
                            polygon[0] = new PointF(0, -2000);
                            polygon[1] = new PointF(0, 500);
                            polygon[2] = new PointF(20, 500);
                            polygon[3] = new PointF(20, -2000);
                            g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), polygon);


                            for (int i = 0; i < Value + 1000; i++)
                            {
                                line[0] = new PointF(0, -i * 20);
                                line[1] = new PointF(-10, -i * 20);

                                g.DrawLine(_WhitePen, line[0], line[1]);

                                if (i % 2 == 0)
                                {
                                    str = (i * 10).ToString("D2");
                                    font = new Font("Times NewRoman", 12f);
                                    len = g.MeasureString(str, font).Width;
                                    g.DrawString(str, font, Brushes.White, -len - 15, -i * 20f - 10);
                                }
                            }

                            for (int i = -1; i > -50; i--)
                            {
                                line[0] = new PointF(10, -i * 10);
                                line[1] = new PointF(0, -i * 10);

                                g.DrawLine(_RedPen, line[0], line[1]);
                            }

                            len = g.MeasureString("000", font).Width + 30;
                            height = (g.MeasureString("000", font).Height + 10) / 2f;
                            polygon = new PointF[8];
                            polygon[0] = new PointF(-15, 0 - _Value * 2f);
                            polygon[1] = new PointF(-20, 5 - _Value * 2f);
                            polygon[2] = new PointF(-20, height - _Value * 2f);
                            polygon[3] = new PointF(-len, height - _Value * 2f);
                            polygon[4] = new PointF(-len, -height - _Value * 2f);
                            polygon[5] = new PointF(-20, -height - _Value * 2f);
                            polygon[6] = new PointF(-20, -5 - _Value * 2f);
                            polygon[7] = new PointF(-15, 0 - _Value * 2f);
                            g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), polygon);
                            g.DrawLines(_WhitePen, polygon);
                            g.DrawString(((int)_Value).ToString("D3"), font, Brushes.White, -len + 5, -10 - _Value * 2f);

                            float basePointY = -_SetValue * 2f;

                            if (basePointY > -_Value * 2f + this.Height / 2f)
                                basePointY = -_Value * 2f + this.Height / 2f;
                            else if (basePointY < -_Value * 2f - this.Height / 2f)
                                basePointY = -_Value * 2f - this.Height / 2f;

                            polygon = new PointF[6];
                            polygon[0] = new PointF(-10, basePointY);
                            polygon[1] = new PointF(0, basePointY - 5);
                            polygon[2] = new PointF(18, basePointY - 5);
                            polygon[3] = new PointF(18, basePointY + 5);
                            polygon[4] = new PointF(0, basePointY + 5);
                            polygon[5] = new PointF(-10, basePointY);
                            g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), polygon);
                            g.DrawLines(_PinkPen, polygon);
                        }
                        else
                        {
                            g.TranslateTransform(0, (float)this.Value * 0.4f + this.Height / 2f);

                            // 绘制近地警告标识
                            polygon = new PointF[4];
                            polygon[0] = new PointF(0, -_GroundAltitudeValue * 0.4f);
                            polygon[1] = new PointF(50, -_GroundAltitudeValue * 0.4f);
                            polygon[2] = new PointF(50, 20 - _GroundAltitudeValue * 0.4f);
                            polygon[3] = new PointF(0, 20 - _GroundAltitudeValue * 0.4f);
                            System.Drawing.Drawing2D.HatchBrush myHatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LightDownwardDiagonal, Color.Gold, Color.Transparent);
                            g.FillPolygon(myHatchBrush, polygon, System.Drawing.Drawing2D.FillMode.Alternate);
                            g.DrawLines(Pens.Yellow, polygon);

                            // 绘制正向高度尺
                            for (int i = 0; i < Value / 100 + 500; i++)
                            {
                                line[0] = new PointF(0, -i * 40);
                                line[1] = new PointF(10, -i * 40);

                                g.DrawLine(_WhitePen, line[0], line[1]);

                                if (i % 2 == 0)
                                {
                                    str = (i * 100).ToString("D3");
                                    g.DrawString(str, font, Brushes.White, 15, -i * 40f - 10);
                                }
                            }
                            // 绘制负向高度尺
                            for (int i = -1; i > -300; i--)
                            {
                                line[0] = new PointF(0, -i * 40);
                                line[1] = new PointF(10, -i * 40);

                                g.DrawLine(_WhitePen, line[0], line[1]);

                                if (i % 2 == 0)
                                {
                                    str = (i * 100).ToString("D3");
                                    g.DrawString(str, font, Brushes.White, 15, -i * 40f - 10);
                                }
                            }

                            // 绘制设置高度
                            polygon = new PointF[8];
                            float basePointY;
                            basePointY = -_SetValue * 0.4f;

                            if (basePointY > -_Value * 0.4f + this.Height / 2f)
                                basePointY = -_Value * 0.4f + this.Height / 2f;
                            else if (basePointY < -_Value * 0.4f - this.Height / 2f)
                                basePointY = -_Value * 0.4f - this.Height / 2f;

                            polygon[0] = new PointF(7, basePointY);
                            polygon[1] = new PointF(2, basePointY - 5);
                            polygon[2] = new PointF(2, basePointY - 25);
                            polygon[3] = new PointF(35, basePointY - 25);
                            polygon[4] = new PointF(35, basePointY + 25);
                            polygon[5] = new PointF(2, basePointY + 25);
                            polygon[6] = new PointF(2, basePointY + 5);
                            polygon[7] = new PointF(7, basePointY);
                            g.DrawLines(_PinkPen, polygon);

                            // 绘制高度读数
                            len = g.MeasureString("0000", font).Width + 32;
                            height = (g.MeasureString("0000", font).Height + 10) / 2f;
                            polygon[0] = new PointF(12, 0 - _Value * 0.4f);
                            polygon[1] = new PointF(20, 5 - _Value * 0.4f);
                            polygon[2] = new PointF(20, height - _Value * 0.4f);
                            polygon[3] = new PointF(len, height - _Value * 0.4f);
                            polygon[4] = new PointF(len, -height - _Value * 0.4f);
                            polygon[5] = new PointF(20, -height - _Value * 0.4f);
                            polygon[6] = new PointF(20, -5 - _Value * 0.4f);
                            polygon[7] = new PointF(12, 0 - _Value * 0.4f);
                            g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), polygon);
                            g.DrawLines(_WhitePen, polygon);
                            g.DrawString(((int)_Value).ToString("D4"), font, Brushes.White, 22, -10 - _Value * 0.4f);


                        }
                        g.ResetTransform();
                    }
                    //85, 278
                    //e.Graphics.ScaleTransform(this.Width/85f, this.Height/278f);
                    this.Image = bmp;
                    e.Graphics.DrawImage(bmp, 0, 0);
                    
                }
                base.OnPaint(e);
            }

        }
    }
}